#include "rtmptask.h"
#include <qDebug>
#include <dlfcn.h> /* 必须加这个头文件 */
#include "unistd.h"
RtmpTask::RtmpTask(Receive_handler *pHandler):m_pHandler(pHandler)
{
    m_pack_num1 = 0;
    m_pack_num2 = 0;

    pSo_handler = dlopen("librtmp.so", RTLD_NOW);

    pRTMP_Alloc         =(RTMP *  (*)(void))dlsym(pSo_handler, "RTMP_Alloc");
    pRTMP_Init          =(void    (*)(RTMP *r))dlsym(pSo_handler, "RTMP_Init");
    pRTMP_SetupURL      =(int     (*)(RTMP *r, char *url))dlsym(pSo_handler, "RTMP_SetupURL");
    pRTMP_Free          =(void    (*)(RTMP *r))dlsym(pSo_handler, "RTMP_Free");
    pRTMP_Close         =(void    (*)(RTMP *r))dlsym(pSo_handler, "RTMP_Close");
    pRTMP_EnableWrite   =(void    (*)(RTMP *r))dlsym(pSo_handler, "RTMP_EnableWrite");
    pRTMP_SetBufferMS   =(void    (*)(RTMP *r, int size))dlsym(pSo_handler, "RTMP_SetBufferMS");
    pRTMP_Connect       =(int     (*)(RTMP *r, RTMPPacket *cp))dlsym(pSo_handler, "RTMP_Connect");
    pRTMP_ConnectStream =(int     (*)(RTMP *r, int seekTime))dlsym(pSo_handler, "RTMP_ConnectStream");
    pRTMP_Read          =(int     (*)(RTMP *r, char *buf, int size))dlsym(pSo_handler, "RTMP_Read");


    qDebug(QString(" pRTMP_Alloc:").append(QString::number((long)pRTMP_Alloc)).toLocal8Bit().data());
    qDebug(QString(" pRTMP_Init:").append(QString::number((long)pRTMP_Init)).toLocal8Bit().data());
    qDebug(QString(" pRTMP_SetupURL:").append(QString::number((long)pRTMP_SetupURL)).toLocal8Bit().data());
    qDebug(QString(" pRTMP_Free:").append(QString::number((long)pRTMP_Free)).toLocal8Bit().data());
    qDebug(QString(" pRTMP_Close:").append(QString::number((long)pRTMP_Close)).toLocal8Bit().data());
    qDebug(QString(" pRTMP_EnableWrite:").append(QString::number((long)pRTMP_EnableWrite)).toLocal8Bit().data());
    qDebug(QString(" pRTMP_SetBufferMS:").append(QString::number((long)pRTMP_SetBufferMS)).toLocal8Bit().data());
    qDebug(QString(" pRTMP_Connect:").append(QString::number((long)pRTMP_Connect)).toLocal8Bit().data());
    qDebug(QString(" pRTMP_ConnectStream:").append(QString::number((long)pRTMP_ConnectStream)).toLocal8Bit().data());
    qDebug(QString(" pRTMP_Read:").append(QString::number((long)pRTMP_Read)).toLocal8Bit().data());

    char aacFileName[100]={0};
    sprintf(aacFileName,"/sdcard/test6.flv");//  /storage/sdcard0/new23.aac

    //outfile = fopen(aacFileName, "wb");

    rtmp = pRTMP_Alloc();

    bExit = false;
}

RtmpTask::~RtmpTask()
{
    dlclose(pSo_handler);

    //if(outfile)
       // fclose(outfile);
}

int RtmpTask::close()
{
    bExit = true;

    wait();
    return 0;
}
int RtmpTask::svc()
{
    bExit = false;

    int nRead;

    int bufsize=1024*1024*10;
    char *buf=(char*)malloc(bufsize);
    memset(buf,0,bufsize);
    long countbufsize=0;

    pRTMP_Init(rtmp);

    rtmp->Link.timeout=10;

    if(!pRTMP_SetupURL(rtmp,"rtmp://ftv.sun0769.com/dgrtv1/mp4:b1"))
    {
        pRTMP_Free(rtmp);
        return -1;
    }

    rtmp->Link.lFlags|=RTMP_LF_LIVE;

     //1hour
    pRTMP_SetBufferMS(rtmp, 3600*1000);


     if(!pRTMP_Connect(rtmp,NULL)){
         qDebug(QString("Connect Err").toLocal8Bit().data());
         pRTMP_Free(rtmp);
         return -1;
     }

     if(!pRTMP_ConnectStream(rtmp,0)){
         qDebug(QString("ConnectStream Err").toLocal8Bit().data());
         pRTMP_Free(rtmp);
         pRTMP_Close(rtmp);
         return -1;
     }
     while(nRead = pRTMP_Read(rtmp,buf,bufsize))
     {
         //fwrite(buf,1,nRead,outfile);
         if(m_pHandler)
         {
             m_pHandler->receive_handler(buf,nRead);
         }
         countbufsize+=nRead;
         if(bExit)
              break;
     }

     usleep(1000000*10);
     if(buf)
     {
         free(buf);
     }

     if(rtmp)
     {
         pRTMP_Close(rtmp);
         pRTMP_Free(rtmp);
         rtmp=NULL;
     }
     qDebug(QString("return 0;").toLocal8Bit().data());
     return 0;
}

